package com.utils;

import com.bean.ListNode;

import java.util.ArrayList;
import java.util.List;

public class DataUtils {

    public static void printArray(int[] nums) {
        if (null == nums) {
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + "  ");
        }
        System.out.println();
        System.out.println();
    }

    public static void printArrayString(String[] nums) {
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + "  ");
        }
        System.out.println();
        System.out.println();
    }

    public static void printArray(int[] nums, int count) {
        if (null == nums) {
            return;
        }

        for (int i = 0; i < count; i++) {
            System.out.print(nums[i] + "  ");
        }
        System.out.println();
        System.out.println();
    }

    public static void printList(List<List<Integer>> result) {
        if (null == result) {
            return;
        }

        for (int i = 0; i < result.size(); i++) {
            for (int t : result.get(i)) {
                System.out.print(t + " ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
    }

    public static void printListListString(List<List<String>> result) {
        if (null == result) {
            return;
        }

        for (int i = 0; i < result.size(); i++) {
            for (String t : result.get(i)) {
                System.out.print(t + " \n");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
    }

    public static void printListListInteger(List<List<Integer>> result) {
        if (null == result) {
            return;
        }

        for (int i = 0; i < result.size(); i++) {
            for (Integer t : result.get(i)) {
                System.out.print(t + " ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
    }

    public static void printListString(List<String> result) {
        if (null == result) {
            return;
        }

        for (int i = 0; i < result.size(); i++) {
            System.out.print(result.get(i));
            System.out.println();
        }
        System.out.println();
        System.out.println();
    }

    public static ListNode getOneToNumberListNode(int num) {
        ListNode headNode = new ListNode(1);
        ListNode currentNode = headNode;
        for (int i = 2; i < num + 1; i++) {
            currentNode.next = new ListNode(i);
            currentNode = currentNode.next;
        }
        return headNode;
    }

    public static ListNode getListNode(int[] num) {
        ListNode headNode = new ListNode(num[0]);
        ListNode currentNode = headNode;
        for (int i = 1; i < num.length; i++) {
            currentNode.next = new ListNode(num[i]);
            currentNode = currentNode.next;
        }
        return headNode;
    }


    public static ListNode getToNumberListNode(int first, int num) {
        ListNode headNode = new ListNode(first);
        ListNode currentNode = headNode;
        for (int i = 1; i < num; i++) {
            currentNode.next = new ListNode(first + i);
            currentNode = currentNode.next;
        }
        return headNode;
    }

    public static void printListNode(ListNode listNode) {
        while (listNode != null) {
            System.out.print(listNode.val + " ");
            listNode = listNode.next;
        }
        System.out.println();
    }

    public static void printDoubleArrayByInt(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void printDoubleArrayByChar(char[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    public static List<List<Integer>> string2ListListInteger(String s) {
        List<List<Integer>> list = new ArrayList<>();
        if (s == null || s.length() == 0) {
            return list;
        }
        s = s.substring(2, s.length() - 2);
        String[] temp = s.split("],\\[");

        for (int i = 0; i < temp.length; i++) {
            String[] temp2 = temp[i].split(",");
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (int j = 0; j < temp2.length; j++) {
                arrayList.add(Integer.valueOf(temp2[j]));
            }
            list.add(arrayList);
        }
        return list;
    }

    public static int[][] string2DoubleArrayInteger(String s) {
        List<List<Integer>> resultList = DataUtils.string2ListListInteger(s);
        int[][] result = new int[resultList.size()][resultList.get(0).size()];

        for (int i = 0; i < resultList.size(); i++) {
            for (int j = 0; j < resultList.get(i).size(); j++) {
                result[i][j] = resultList.get(i).get(j);
            }
        }
        return result;
    }

    public static List<List<String>> string2ListListString(String s) {
        List<List<String>> list = new ArrayList<>();
        if (s == null || s.length() == 0) {
            return list;
        }
        s = s.substring(2, s.length() - 2);
        String[] temp = s.split("],\\[");

        for (int i = 0; i < temp.length; i++) {
            String[] temp2 = temp[i].split(",");
            ArrayList<String> arrayList = new ArrayList<>();
            for (int j = 0; j < temp2.length; j++) {
                arrayList.add(temp2[j]);
            }
            list.add(arrayList);
        }
        return list;
    }
}
